<%@page language="java" pageEncoding="utf-8" %>
<%@page isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.elite.km.util.ArticleUtil"%>
<%@page import="org.apache.commons.lang3.StringUtils" %>
<%@page import="com.elite.km.common.Config" %>
<%
    request.setCharacterEncoding("UTF-8");
    String charset = request.getParameter("charset");
    String contextPath = Config.getInstance().getContextPath();

    String preIframeSrc = "";
    if (ArticleUtil.getAbsolutePathFlagBoolean()) {
        preIframeSrc = ArticleUtil.getArticleStoreServerPath();
    } else {
        preIframeSrc = ArticleUtil.getArticleStoreContextPath();
    }
    
    if (StringUtils.isEmpty(charset)) {
        charset = "GBK";
    }
    response.setContentType("text/html; charset=" + charset);
    pageContext.setAttribute("contextPath", contextPath);
    pageContext.setAttribute("preIframeSrc", preIframeSrc);
%>
<html>
<head>
    <meta http-equiv="cache-control" content="max-age=0"/>
    <meta http-equiv="cache-control" content="no-cache"/>
    <meta http-equiv="expires" content="0"/>
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT"/>
    <meta http-equiv="pragma" content="no-cache"/>
    <script src="${contextPath}/js/lib/jquery-3.4.1.min.js"></script>
    <title>KM Article Container</title>
    <link rel="stylesheet" href="${contextPath}/css/viewer.css">
    <style>
        * { margin: 0; padding: 0}
        body { overflow: hidden; }
    </style>
    <script>
		$(function() {
			// 获取URL参数
			function getParameterByName(name) {
				const url = window.location.href;
				name = name.replace(/[\[\]]/g, '\\$&');
				const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
				const results = regex.exec(url);
				if (!results) return null;
				if (!results[2]) return '';
				return decodeURIComponent(results[2].replace(/\+/g, ' '));
			}
			// 关键逻辑：检查是否包含 action=kmToolsExtractFile，若存在则重定向
            const action = getParameterByName('action');
            if (action === 'kmToolsExtractFile') {
                // 将 URL 中的 kmMiddlePage.jsp 替换为 kmToolsEditArticle.jsp
                const currentUrl = window.location.href;
                const newUrl = currentUrl.replace('kmMiddlePage.jsp', 'kmToolsEditArticle.jsp');
                // 执行重定向
                window.location.href = newUrl;
                return; // 终止后续代码，避免重复执行
            }
			
			// 获取targetURL并解码
			const targetURL = getParameterByName('targetURL');
			if (!targetURL) {
				console.error('Missing targetURL parameter');
				return;
			}
			console.log('targetURL原始值:', targetURL);
			
			// 构建完整的iframe src
			const preIframeSrc = '<%=preIframeSrc %>';
			console.log('preIframeSrc:', preIframeSrc);
			
			// 安全编码URL路径的函数
			function encodePath(path) {
				// 分割路径为目录和文件名
				const parts = path.split('/');
				
				// 对每个部分分别编码
				const encodedParts = parts.map(part => encodeURIComponent(part));
				
				// 重新组合路径
				return encodedParts.join('/');
			}
			
			// 对targetURL进行安全编码
			const safeEncodedTargetURL = encodePath(targetURL);
			console.log('安全编码后的targetURL:', safeEncodedTargetURL);
            
            // 在JavaScript中生成时间戳
            const timestamp = new Date().getTime();
            console.log('时间戳:', timestamp);
            
            // 使用明确的变量拼接URL
            const baseUrl = preIframeSrc;
            const fullPath = baseUrl+'/'+safeEncodedTargetURL+'?t='+timestamp;
            console.log('完整URL:', fullPath);
            
            // 设置iframe src
            $('#iframe').attr('src', fullPath);
            
            // 防止iframe缓存
            $('#iframe').one("load", function() {
                $(this).attr('src', $(this).attr('src'));
            });
        });
    </script>
</head>
<body>
    <iframe id="iframe" width="100%" height="100%" style="border: 0;" src="" ></iframe>
</body>
</html>